#!/usr/bin/env Python
# coding=utf-8

import handlers.base as baseHandler
import methods.database.orm.login_user as login_user
import methods.database.orm.room as room
import methods.database.orm.room_player as room_player
import methods.database.orm.active_user as active_user

import json
import time

class IndexHandler(baseHandler.BaseHandler):
    def get(self):
        self.render("login/index.html")

    def post(self):
        login_name = self.get_argument("username")
        print(login_name)
        password = self.get_argument("password")
        data={"login_name":login_name}
        user_infos = login_user.getUser(data)
        if user_infos:
            db_pwd = user_infos.password
            if db_pwd == password:
                self.set_secure_cookie(login_name,db_pwd, httponly=True, secure=True)#将用户名和密码以密文的形式存入cookie，后两个参数是保证cookie只读
                self.set_cookie("xxgame_username",login_name)#将当前登录用户明文保存即可
                #记录活跃用户
                activeUserData = {"username": login_name,"username": login_name,"last_login_time": time.strftime( '%Y-%m-%d %X', time.localtime() ),'active_flag':1}
                print("abcdefgaaa")
                print(activeUserData)
                room_infos = active_user.addUser(activeUserData)
                #如果已开好房，则直接进入房间
                roomData={"username":login_name}
                room_infos = room.getRoomInfo(roomData)
                if(room_infos):
                    self.write({"status":3,"msg":room_infos.room_id})
                else:
                    #再判断是否已加入了房间
                    playerData = {"username": login_name}
                    room_player_infos = room_player.getRoomPlayer(playerData)
                    if (room_player_infos):
                        self.write({"status": 3, "msg": room_player_infos.room_id})
                    else:
                        self.write({"status":0,"msg":"welcome you: " + login_name})
            else:
                self.write({"status":1,"msg":"您的密码不正确."})
        else:
            self.write({"status":2,"msg":"该用户不存在."})

class LoginHandler(baseHandler.BaseHandler):
    def get(self):
        self.render("login/index.html")

    def post(self):
        data = json.loads(self.request.body)
        login_name = data["username"]
        password = data["password"]
        data={"login_name":login_name}
        user_infos = login_user.getUser(data)
        if user_infos:
            db_pwd = user_infos.password
            if db_pwd == password:
                self.set_secure_cookie(login_name,db_pwd, httponly=True, secure=True)#将用户名和密码以密文的形式存入cookie，后两个参数是保证cookie只读
                self.set_cookie("xxgame_username",login_name)#将当前登录用户明文保存即可
                # 记录活跃用户
                activeUserData = {"username": login_name, "username": login_name,"last_login_time": time.strftime('%Y-%m-%d %X', time.localtime()), 'active_flag': 1}
                room_infos = active_user.addUser(activeUserData)
                #如果已开好房，则直接进入房间
                roomData={"username":login_name}
                room_infos = room.getRoomInfo(roomData)
                if(room_infos):
                    self.write({"status":3,"msg":room_infos.room_id})
                else:
                    #再判断是否已加入了房间
                    playerData = {"username": login_name}
                    room_player_infos = room_player.getRoomPlayer(playerData)
                    if (room_player_infos):
                        self.write({"status": 3, "msg": room_player_infos.room_id})
                    else:
                        self.write({"status":0,"msg":"welcome you: " + login_name})
            else:
                self.write({"status":1,"msg":"您的密码不正确."})
        else:
            self.write({"status":2,"msg":"该用户不存在."})

class LogoutHandler(baseHandler.BaseHandler):
    def get(self):
        self.post()

    def post(self):
        data = json.loads(self.request.body)
        login_name = data["username"]
        # 记录活跃用户
        activeUserData = {"username": login_name, "username": login_name,"last_login_time": time.strftime('%Y-%m-%d %X', time.localtime()), 'active_flag': 0}
        active_user.addUser(activeUserData)
        self.clear_cookie("xxgame_username")
        self.write({"status":0,"msg":"注销成功."})